package com.baselibrary.libs.Utils.netUtils.NoHttp.task;

import android.os.SystemClock;
import android.util.Log;
import android.webkit.URLUtil;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * Created by 35876 于萌萌
 * 创建日期: 19:56 . 2017年01月15日
 * 描述:
 * <p>
 * <p>
 * 备注:
 */

public class Net {
    public  static String startNet(String urlstr, String para){
        HttpURLConnection connection = null;
        try {
            if (!URLUtil.isNetworkUrl(urlstr)){
                return "";
            }
            URL url = new URL(urlstr);
            connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("POST");
            connection.setDoOutput(false);
            connection.setConnectTimeout(10 * 1000);
            connection.setReadTimeout(10 * 1000);
            connection.setRequestProperty("Connection", "Keep-Alive");
            connection.setRequestProperty("Charset", "UTF-8");
            //	connection.setRequestProperty("Accept-Encoding", "gzip, deflate");
            connection .setRequestProperty("Accept-Encoding", "identity");
            connection.connect();
            DataOutputStream out = new DataOutputStream(connection.getOutputStream());
            out.writeBytes(para);
            out.flush();
            out.close();
            int responseCode = connection.getResponseCode();
            if (responseCode == 200) {
                ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
                InputStream inputStream = new BufferedInputStream(connection.getInputStream());
                //获取网络文件的大小   .setRequestProperty("Accept-Encoding", "identity");
                //这样设置获取到了正常的 网络文件大小  不能要压缩的
                int total = connection.getContentLength();
                //获取本地文件的大小
                //long total = inputStream.available();
                String totalstr = String.valueOf(total);
                Log.d("文件大小", totalstr);
                int len = 0;
                int length = 0;
                byte[] buffer = new byte[512];
                while ((len = inputStream.read(buffer)) != -1) {
                    arrayOutputStream.write(buffer, 0, len);
                    length += len;
                    //演示进度
                    SystemClock.sleep(1*200);
                    //算出来的值
                    int progress = (int) (length * 100L / total);
                    //onPostUpdate(progress);

                }
                inputStream.close();
                arrayOutputStream.flush();
                inputStream.close();
                return new String(arrayOutputStream.toByteArray());
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (connection != null)
                connection.disconnect();
        }
        return "请求网络失败";
    }
}
